/*
 * CgType.c
 *
 *  Created on: :/:/:
 *      Author: guanxiaoying
 */

#include "../coredef.h"
#include "../cc/cc.h"
#include "cg.h"
typedef struct _LINKTYPE_COMP
{
	UCHAR		ucKind;				/* 2 */
	UCHAR		ucSigned;			/* 3 */
	UCHAR		ucPrecision;		/* 4 */
	UCHAR		ucIntorFloat;		/* 5 */
	UCHAR		ucTypeQualifier;	/* 6 */
	UCHAR       ucVariableSize;
	UCHAR       ucUndefined;
	U4			ulSize;					/* 8 */
	AST			astOrigin;				/* 9 */
	AST         astNoQual;
	AST			astMemChain;		/* 10 */
	AST         astNext;
	AST         astTu;
	PSZ         pszName;
} LINKTYPE_COMP, *PLINKTYPE_COMP;

static AST  cg_addNewTypeInfo( AST astCCType );
static S4  cgTypeCompBitfield_forTree( LINKTYPE_COMP *pstTree, LINKTYPE_COMP* pstNew ){
	if( pstNew->astOrigin > pstTree->astOrigin ){
		return RBT_LARGER;
	}
	if( pstNew->astOrigin < pstTree->astOrigin ){
		return RBT_LESS;
	}
	if( pstNew->ulSize > pstTree->ulSize ){
		return RBT_LARGER;
	}
	if( pstNew->ulSize < pstTree->ulSize ){
		return RBT_LESS;
	}
	if( pstNew->astNoQual > pstTree->astNoQual ){
		return RBT_LARGER;
	}
	if( pstNew->astNoQual < pstTree->astNoQual ){
		return RBT_LESS;
	}
	if( pstNew->ucTypeQualifier > pstTree->ucTypeQualifier ){
		return RBT_LARGER;
	}
	if( pstNew->ucTypeQualifier < pstTree->ucTypeQualifier ){
		return RBT_LESS;
	}
	return RBT_FOUND;
}
static S4  cgTypeCompFunction_forTree( LINKTYPE_COMP *pstTree, LINKTYPE_COMP* pstNew ){

	if( pstNew->ucUndefined > pstTree->ucUndefined ){
		return RBT_LARGER;
	}
	if( pstNew->ucUndefined < pstTree->ucUndefined ){
		return RBT_LESS;
	}
	if( pstNew->astOrigin > pstTree->astOrigin ){
		return RBT_LARGER;
	}
	if( pstNew->astOrigin < pstTree->astOrigin ){
		return RBT_LESS;
	}
	if( pstNew->astMemChain > pstTree->astMemChain ){
		return RBT_LARGER;
	}
	if( pstNew->astMemChain < pstTree->astMemChain ){
		return RBT_LESS;
	}
	return RBT_FOUND;
}
static S4  cgTypeCompStUnEm_forTree( LINKTYPE_COMP *pstTree, LINKTYPE_COMP* pstNew ){
	if( pstNew->astTu > pstTree->astTu ){
		return RBT_LARGER;
	}
	if( pstNew->astTu < pstTree->astTu ){
		return RBT_LESS;
	}
	if( str_length(pstNew->pszName) != 0 && str_length(pstTree->pszName) != 0 ){
		if( strcmp( pstNew->pszName, pstTree->pszName ) > 0 ){
			return RBT_LARGER;
		}
		if( strcmp( pstNew->pszName, pstTree->pszName ) < 0 ){
			return RBT_LESS;
		}
	}else if( str_length(pstNew->pszName) != 0  ){
		return RBT_LARGER;
	}else if( str_length(pstTree->pszName) != 0 ){
		return RBT_LESS;
	}
	if( pstNew->astMemChain > pstTree->astMemChain ){
		return RBT_LARGER;
	}
	if( pstNew->astMemChain < pstTree->astMemChain ){
		return RBT_LESS;
	}
	if( pstNew->ucUndefined > pstTree->ucUndefined ){
		return RBT_LARGER;
	}
	if( pstNew->ucUndefined < pstTree->ucUndefined ){
		return RBT_LESS;
	}
	if( pstNew->ulSize > pstTree->ulSize ){
		return RBT_LARGER;
	}
	if( pstNew->ulSize < pstTree->ulSize ){
		return RBT_LESS;
	}
	if( pstNew->astNoQual > pstTree->astNoQual ){
		return RBT_LARGER;
	}
	if( pstNew->astNoQual < pstTree->astNoQual ){
		return RBT_LESS;
	}
	if( pstNew->ucTypeQualifier > pstTree->ucTypeQualifier ){
		return RBT_LARGER;
	}
	if( pstNew->ucTypeQualifier < pstTree->ucTypeQualifier ){
		return RBT_LESS;
	}
	return RBT_FOUND;
}
static S4  cgTypeCompPointer_forTree( LINKTYPE_COMP *pstTree, LINKTYPE_COMP* pstNew ){
	if( pstNew->astOrigin > pstTree->astOrigin ){
		return RBT_LARGER;
	}
	if( pstNew->astOrigin < pstTree->astOrigin ){
		return RBT_LESS;
	}
	if( pstNew->ulSize > pstTree->ulSize ){
		return RBT_LARGER;
	}
	if( pstNew->ulSize < pstTree->ulSize ){
		return RBT_LESS;
	}
	if( pstNew->astNoQual > pstTree->astNoQual ){
		return RBT_LARGER;
	}
	if( pstNew->astNoQual < pstTree->astNoQual ){
		return RBT_LESS;
	}
	if( pstNew->ucTypeQualifier > pstTree->ucTypeQualifier ){
		return RBT_LARGER;
	}
	if( pstNew->ucTypeQualifier < pstTree->ucTypeQualifier ){
		return RBT_LESS;
	}
	return RBT_FOUND;
}
static S4  cgTypeCompArray_forTree( LINKTYPE_COMP *pstTree, LINKTYPE_COMP* pstNew ){
	if( pstNew->astOrigin > pstTree->astOrigin ){
		return RBT_LARGER;
	}
	if( pstNew->astOrigin < pstTree->astOrigin ){
		return RBT_LESS;
	}
	if( pstNew->ucUndefined > pstTree->ucUndefined ){
		return RBT_LARGER;
	}
	if( pstNew->ucUndefined < pstTree->ucUndefined ){
		return RBT_LESS;
	}
	if( pstNew->ucVariableSize > pstTree->ulSize ){
		return RBT_LARGER;
	}
	if( pstNew->ucVariableSize < pstTree->ulSize ){
		return RBT_LESS;
	}
	if( pstNew->ulSize > pstTree->ulSize ){
		return RBT_LARGER;
	}
	if( pstNew->ulSize < pstTree->ulSize ){
		return RBT_LESS;
	}
	if( pstNew->astNoQual > pstTree->astNoQual ){
		return RBT_LARGER;
	}
	if( pstNew->astNoQual < pstTree->astNoQual ){
		return RBT_LESS;
	}
	if( pstNew->ucTypeQualifier > pstTree->ucTypeQualifier ){
		return RBT_LARGER;
	}
	if( pstNew->ucTypeQualifier < pstTree->ucTypeQualifier ){
		return RBT_LESS;
	}
	return RBT_FOUND;
}
static S4  cgTypeCompBasic_forTree( LINKTYPE_COMP *pstTree, LINKTYPE_COMP* pstNew ){
	if( pstNew->ulSize > pstTree->ulSize ){
		return RBT_LARGER;
	}
	if( pstNew->ulSize < pstTree->ulSize ){
		return RBT_LESS;
	}
	if( pstNew->ucIntorFloat > pstTree->ucIntorFloat ){
		return RBT_LARGER;
	}
	if( pstNew->ucIntorFloat < pstTree->ucIntorFloat ){
		return RBT_LESS;
	}
	if( pstNew->ucPrecision > pstTree->ucPrecision ){
		return RBT_LARGER;
	}
	if( pstNew->ucPrecision < pstTree->ucPrecision ){
		return RBT_LESS;
	}
	if( pstNew->ucSigned > pstTree->ucSigned ){
		return RBT_LARGER;
	}
	if( pstNew->ucSigned < pstTree->ucSigned ){
		return RBT_LESS;
	}
	if( pstNew->ucTypeQualifier > pstTree->ucTypeQualifier ){
		return RBT_LARGER;
	}
	if( pstNew->ucTypeQualifier < pstTree->ucTypeQualifier ){
		return RBT_LESS;
	}
	if( pstNew->astNoQual > pstTree->astNoQual ){
		return RBT_LARGER;
	}
	if( pstNew->astNoQual < pstTree->astNoQual ){
		return RBT_LESS;
	}
	return RBT_FOUND;
}
static S4  cgTypeComp_forTree( AST	astLinkType, VOID* pTypeBuf ){
	PLINKTYPE_COMP  pst = (PLINKTYPE_COMP)pTypeBuf;
	LINKTYPE_COMP   stTree={0};
	stTree.astMemChain = stLinkTypeInfoAccess.memChain.get( astLinkType );
	stTree.astNext = stLinkTypeInfoAccess.next.get( astLinkType );
	stTree.astNoQual = stLinkTypeInfoAccess.noQualType.get( astLinkType );
	stTree.astOrigin = stLinkTypeInfoAccess.elementType.get( astLinkType );
	stTree.ucIntorFloat = stLinkTypeInfoAccess.intFloat.get( astLinkType );
	stTree.ucKind = stLinkTypeInfoAccess.typeKind.get( astLinkType );
	stTree.ucPrecision = stLinkTypeInfoAccess.longShortFlg.get( astLinkType );
	stTree.ucSigned = stLinkTypeInfoAccess.signFlag.get( astLinkType );
	stTree.ucTypeQualifier = stLinkTypeInfoAccess.typeQual.get( astLinkType );
	stTree.ucVariableSize = stLinkTypeInfoAccess.bVariableSize.get( astLinkType );
	stTree.ulSize = stLinkTypeInfoAccess.size.get( astLinkType );
	stTree.pszName = stLinkTypeInfoAccess.tagName.get( astLinkType );
	if( pst->ucKind > stTree.ucKind ){
		return RBT_LARGER;
	}
	if( pst->ucKind < stTree.ucKind ){
		return RBT_LESS;
	}
	switch( stTree.ucKind ){
		case TYPEKIND_BASIC			:	/* 通常の型			*/
			return cgTypeCompBasic_forTree( &stTree, pst );
		case TYPEKIND_ARRAY			:	/* 配列				*/
			return cgTypeCompArray_forTree( &stTree, pst );
		case TYPEKIND_POINTER		:	/* ﾎﾟｲﾝﾀ			*/
			return cgTypeCompPointer_forTree( &stTree, pst );
		case TYPEKIND_STRUCT		:	/* 構造体			*/
		case TYPEKIND_UNION			:	/* 共用体			*/
		case TYPEKIND_ENUM			:	/* 列挙型			*/
			return cgTypeCompStUnEm_forTree( &stTree, pst );
		case TYPEKIND_FUNCTION		:	/* 関数(実体)			*/
			return cgTypeCompFunction_forTree( &stTree, pst );
		case TYPEKIND_BITFIELD		:	/* BitField型			*/
			return cgTypeCompBitfield_forTree( &stTree, pst );
	}
	return RBT_LARGER;
}
static AST cg_ConvertBitfieldTypeInfo( AST astCCType ){
	AST  astLinkType=0;
	AST  astNoQualCc = 0;
	AST  astNoQualLink = 0;
	AST  astNewRelation;
	AST  astEleType;
	AST  astEleLinkType;
	AST  astNext;
	AST  astCurr;
	LINKTYPE_COMP   stTree={0};
	BOOL bRet;
	/* added ? */
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astNewRelation );
	if( bRet == TRUE ){
		astLinkType = stCgTypeRelationAccess.linkedType.get( astNewRelation );
		if( astLinkType != 0 ){
			return astLinkType;
		}
	}
	/* new one  */
	stLinkTypeInfoAccess.alloc(1);
	astLinkType = stLinkTypeInfoAccess.getMax();

	astNewRelation = stCgTypeRelationAccess.addCcType( astCCType );
	stCgTypeRelationAccess.linkedType.set( astNewRelation, astLinkType );

	/* no qualifier type  */
	astNoQualCc = stTypeInfoAccess.noQualType.get( astCCType );
	if( astNoQualCc ){
		astNoQualLink = cg_addNewTypeInfo( astNoQualCc );
		stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
		astNext = stLinkTypeInfoAccess.next.get( astNoQualLink );
		astCurr = astNoQualLink;
		while( astNext ){
			astCurr = astNext;
			astNext = stLinkTypeInfoAccess.next.get( astCurr );
		}
		stLinkTypeInfoAccess.next.set( astCurr, astLinkType );
	}
	/* element  */
	astEleType = stTypeInfoAccess.elementType.get( astCCType );
	astEleLinkType = cg_addNewTypeInfo( astEleType );
	/* add to tree  */
//	stTree.astMemChain = stTypeInfoAccess.memChain.get( astCCType );
//	stTree.astNext = stTypeInfoAccess.next.get( astCCType );
	stTree.astNoQual = astNoQualLink;
	stTree.astOrigin = astEleLinkType;
//	stTree.ucIntorFloat = stTypeInfoAccess.intFloat.get( astCCType );
	stTree.ucKind = stTypeInfoAccess.typeKind.get( astCCType );
//	stTree.ucPrecision = stTypeInfoAccess.longShortFlg.get( astCCType );
//	stTree.ucSigned = stTypeInfoAccess.signFlag.get( astCCType );
	stTree.ucTypeQualifier = stTypeInfoAccess.typeQual.get( astCCType );
//	stTree.ucVariableSize = stTypeInfoAccess.bVariableSize.get( astCCType );
	stTree.ulSize = stTypeInfoAccess.size.get( astCCType );
//	stTree.astTu = CCoreGetCurrTranslatioUnit();
	astLinkType = stLinkTypeInfoAccess.addTypeInfo( astLinkType, &stTree, cgTypeComp_forTree );

	stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
	stLinkTypeInfoAccess.typeKind.set( astLinkType, stTree.ucKind  );
	stLinkTypeInfoAccess.typeQual.set( astLinkType, stTree.ucTypeQualifier  );
	stLinkTypeInfoAccess.size.set( astLinkType, stTree.ulSize );
	stLinkTypeInfoAccess.elementType.set( astLinkType, astEleLinkType );

	return astLinkType;
}
static AST cg_ConvertFunctionTypeInfo( AST astCCType ){
	AST  astLinkType=0;
	AST  astNewRelation;
	AST  astMember;
	AST  astMemSym;
	AST  astMemHeader;
	AST  astMemPreOne;
	AST  astMemChain;
	AST  astRetType;
	AST  astRetLinkType;
	LINKTYPE_COMP   stTree={0};
	BOOL bRet;
	/* added ? */
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astNewRelation );
	if( bRet == TRUE ){
		astLinkType = stCgTypeRelationAccess.linkedType.get( astNewRelation );
		if( astLinkType != 0 ){
			return astLinkType;
		}
	}
	/* new one  */
	stLinkTypeInfoAccess.alloc(1);
	astLinkType = stLinkTypeInfoAccess.getMax();

	astNewRelation = stCgTypeRelationAccess.addCcType( astCCType );
	stCgTypeRelationAccess.linkedType.set( astNewRelation, astLinkType );

	/* no qualifier type  */
//	astNoQualCc = stTypeInfoAccess.noQualType.get( astCCType );
//	if( astNoQualCc ){
//		astNoQualLink = cg_ConvertBasicTypeInfo( astNoQualCc );
//		stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
//		astNext = stLinkTypeInfoAccess.next.get( astNoQualLink );
//		astCurr = astNoQualLink;
//		while( astNext ){
//			astCurr = astNext;
//			astNext = stLinkTypeInfoAccess.next.get( astCurr );
//		}
//		stLinkTypeInfoAccess.next.set( astCurr, astLinkType );
//	}
	/* element  */
	astMemChain = stTypeInfoAccess.memChain.get( astCCType );
	astMemHeader = 0;
	astMemPreOne = 0;
	while( astMemChain ){
		astMemSym = stUniteChainAccess.parameter_sym.get( astMemChain );
		astMember = CG_ConvertMembers( astMemSym );
		if( astMemHeader == 0 ){
			astMemHeader = astMember;
			astMemPreOne = astMember;
		}else{
			stMemberSymbolAccess.next.set( astMemPreOne, astMember );
		}
		astMemChain = stUniteChainAccess.next.get( astMemChain );
	}
	astRetType = stTypeInfoAccess.elementType.get( astCCType );
	astRetLinkType = cg_addNewTypeInfo( astRetType );
	/* add to tree  */
	stTree.astMemChain = astMemHeader;
//	stTree.astNext = stTypeInfoAccess.next.get( astCCType );
//	stTree.astNoQual = astNoQualLink;
	stTree.astOrigin = astRetLinkType;
//	stTree.ucIntorFloat = stTypeInfoAccess.intFloat.get( astCCType );
	stTree.ucKind = stTypeInfoAccess.typeKind.get( astCCType );
//	stTree.ucPrecision = stTypeInfoAccess.longShortFlg.get( astCCType );
//	stTree.ucSigned = stTypeInfoAccess.signFlag.get( astCCType );
//	stTree.ucTypeQualifier = stTypeInfoAccess.typeQual.get( astCCType );
//	stTree.ucVariableSize = stTypeInfoAccess.bVariableSize.get( astCCType );
//	stTree.ulSize = stTypeInfoAccess.size.get( astCCType );
//	stTree.astTu = CCoreGetCurrTranslatioUnit();
//	stTree.pszName = stSymbolInfoAccess.name.get( stTypeInfoAccess.tagInfo.get( astCCType ) );
	stTree.ucUndefined = stTypeInfoAccess.bSizeUnDefined.get( astCCType );
	astLinkType = stLinkTypeInfoAccess.addTypeInfo( astLinkType, &stTree, cgTypeComp_forTree );

	stLinkTypeInfoAccess.memChain.set( astLinkType, astMemHeader );
	stLinkTypeInfoAccess.elementType.set( astLinkType, astRetLinkType );
	stLinkTypeInfoAccess.typeKind.set( astLinkType, stTree.ucKind  );
	stLinkTypeInfoAccess.bSizeUnDefined.set( astLinkType, stTree.ucUndefined );

	return astLinkType;
}
static AST cg_ConvertStUnEmTypeInfo( AST astCCType ){
	AST  astLinkType=0;
	AST  astNoQualCc = 0;
	AST  astNoQualLink = 0;
	AST  astNewRelation;
	AST  astMember;
	AST  astMemSym;
	AST  astMemHeader;
	AST  astMemPreOne;
	AST  astMemChain;
	AST  astNext;
	AST  astCurr;
	LINKTYPE_COMP   stTree={0};
	BOOL bRet;
	/* added ? */
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astNewRelation );
	if( bRet == TRUE ){
		astLinkType = stCgTypeRelationAccess.linkedType.get( astNewRelation );
		if( astLinkType != 0 ){
			return astLinkType;
		}
	}
	/* new one  */
	stLinkTypeInfoAccess.alloc(1);
	astLinkType = stLinkTypeInfoAccess.getMax();

	astNewRelation = stCgTypeRelationAccess.addCcType( astCCType );
	stCgTypeRelationAccess.linkedType.set( astNewRelation, astLinkType );

	/* no qualifier type  */
	astNoQualCc = stTypeInfoAccess.noQualType.get( astCCType );
	if( astNoQualCc ){
		astNoQualLink = cg_addNewTypeInfo( astNoQualCc );
		stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
		astNext = stLinkTypeInfoAccess.next.get( astNoQualLink );
		astCurr = astNoQualLink;
		while( astNext ){
			astCurr = astNext;
			astNext = stLinkTypeInfoAccess.next.get( astCurr );
		}
		stLinkTypeInfoAccess.next.set( astCurr, astLinkType );
	}
	/* element  */
	astMemChain = stTypeInfoAccess.memChain.get( astCCType );
	astMemHeader = 0;
	astMemPreOne = 0;
	while( astMemChain ){
		astMemSym = stUniteChainAccess.parameter_sym.get( astMemChain );
		astMember = CG_ConvertMembers( astMemSym );
		if( astMemHeader == 0 ){
			astMemHeader = astMember;
		}else{
			stMemberSymbolAccess.next.set( astMemPreOne, astMember );
		}
		astMemPreOne = astMember;
		astMemChain = stUniteChainAccess.next.get( astMemChain );
	}
	/* add to tree  */
	stTree.astMemChain = astMemHeader;
//	stTree.astNext = stTypeInfoAccess.next.get( astCCType );
	stTree.astNoQual = astNoQualLink;
//	stTree.astOrigin = astEleLinkType;
//	stTree.ucIntorFloat = stTypeInfoAccess.intFloat.get( astCCType );
	stTree.ucKind = stTypeInfoAccess.typeKind.get( astCCType );
//	stTree.ucPrecision = stTypeInfoAccess.longShortFlg.get( astCCType );
//	stTree.ucSigned = stTypeInfoAccess.signFlag.get( astCCType );
	stTree.ucTypeQualifier = stTypeInfoAccess.typeQual.get( astCCType );
//	stTree.ucVariableSize = stTypeInfoAccess.bVariableSize.get( astCCType );
	stTree.ulSize = stTypeInfoAccess.size.get( astCCType );
	stTree.astTu = CCoreGetCurrTranslatioUnit();
	stTree.pszName = stSymbolInfoAccess.name.get( stTypeInfoAccess.tagInfo.get( astCCType ) );
	stTree.ucUndefined = stTypeInfoAccess.bSizeUnDefined.get( astCCType );

	astLinkType = stLinkTypeInfoAccess.addTypeInfo( astLinkType, &stTree, cgTypeComp_forTree );
	stLinkTypeInfoAccess.memChain.set( astLinkType, astMemHeader );
	stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
	stLinkTypeInfoAccess.typeKind.set( astLinkType, stTree.ucKind  );
	stLinkTypeInfoAccess.typeQual.set( astLinkType, stTree.ucTypeQualifier  );
	stLinkTypeInfoAccess.size.set( astLinkType, stTree.ulSize );
	stLinkTypeInfoAccess.cnter.set( astLinkType, stTree.astTu );
	stLinkTypeInfoAccess.bSizeUnDefined.set( astLinkType, stTree.ucUndefined );
	stLinkTypeInfoAccess.tagName.set( astLinkType, stTree.pszName );

	return astLinkType;
}
static AST cg_ConvertPointerTypeInfo( AST astCCType ){
	AST  astLinkType=0;
	AST  astNoQualCc = 0;
	AST  astNoQualLink = 0;
	AST  astNewRelation;
	AST  astEleType;
	AST  astEleLinkType;
	AST  astNext;
	AST  astCurr;
	LINKTYPE_COMP   stTree={0};
	BOOL bRet;
	/* added ? */
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astNewRelation );
	if( bRet == TRUE ){
		astLinkType = stCgTypeRelationAccess.linkedType.get( astNewRelation );
		if( astLinkType != 0 ){
			return astLinkType;
		}
	}
	/* new one  */
	stLinkTypeInfoAccess.alloc(1);
	astLinkType = stLinkTypeInfoAccess.getMax();

	astNewRelation = stCgTypeRelationAccess.addCcType( astCCType );
	stCgTypeRelationAccess.linkedType.set( astNewRelation, astLinkType );

	/* no qualifier type  */
	astNoQualCc = stTypeInfoAccess.noQualType.get( astCCType );
	if( astNoQualCc ){
		astNoQualLink = cg_addNewTypeInfo( astNoQualCc );
		stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
		astNext = stLinkTypeInfoAccess.next.get( astNoQualLink );
		astCurr = astNoQualLink;
		while( astNext ){
			astCurr = astNext;
			astNext = stLinkTypeInfoAccess.next.get( astCurr );
		}
		stLinkTypeInfoAccess.next.set( astCurr, astLinkType );
	}
	/* element  */
	astEleType = stTypeInfoAccess.elementType.get( astCCType );
	astEleLinkType = cg_addNewTypeInfo( astEleType );
	/* add to tree  */
//	stTree.astMemChain = stTypeInfoAccess.memChain.get( astCCType );
//	stTree.astNext = stTypeInfoAccess.next.get( astCCType );
	stTree.astNoQual = astNoQualLink;
	stTree.astOrigin = astEleLinkType;
//	stTree.ucIntorFloat = stTypeInfoAccess.intFloat.get( astCCType );
	stTree.ucKind = stTypeInfoAccess.typeKind.get( astCCType );
//	stTree.ucPrecision = stTypeInfoAccess.longShortFlg.get( astCCType );
//	stTree.ucSigned = stTypeInfoAccess.signFlag.get( astCCType );
	stTree.ucTypeQualifier = stTypeInfoAccess.typeQual.get( astCCType );
//	stTree.ucVariableSize = stTypeInfoAccess.bVariableSize.get( astCCType );
	stTree.ulSize = stTypeInfoAccess.size.get( astCCType );
//	stTree.astTu = CCoreGetCurrTranslatioUnit();
	astLinkType = stLinkTypeInfoAccess.addTypeInfo( astLinkType, &stTree, cgTypeComp_forTree );

	stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
	stLinkTypeInfoAccess.elementType.set( astLinkType, astEleLinkType );
	stLinkTypeInfoAccess.typeKind.set( astLinkType, stTree.ucKind  );
	stLinkTypeInfoAccess.typeQual.set( astLinkType, stTree.ucTypeQualifier  );
	stLinkTypeInfoAccess.size.set( astLinkType, stTree.ulSize );

	return astLinkType;
}
static AST cg_ConvertArrayTypeInfo( AST astCCType ){
	AST  astLinkType=0;
	AST  astNoQualCc = 0;
	AST  astNoQualLink = 0;
	AST  astNewRelation;
	AST  astEleType;
	AST  astEleLinkType;
	AST  astNext;
	AST  astCurr;
	LINKTYPE_COMP   stTree={0};
	BOOL bRet;
	/* added ? */
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astNewRelation );
	if( bRet == TRUE ){
		astLinkType = stCgTypeRelationAccess.linkedType.get( astNewRelation );
		if( astLinkType != 0 ){
			return astLinkType;
		}
	}
	/* new one  */
	stLinkTypeInfoAccess.alloc(1);
	astLinkType = stLinkTypeInfoAccess.getMax();

	astNewRelation = stCgTypeRelationAccess.addCcType( astCCType );
	stCgTypeRelationAccess.linkedType.set( astNewRelation, astLinkType );

	/* no qualifier type  */
	astNoQualCc = stTypeInfoAccess.noQualType.get( astCCType );
	if( astNoQualCc ){
		astNoQualLink = cg_addNewTypeInfo( astNoQualCc );
		stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
		astNext = stLinkTypeInfoAccess.next.get( astNoQualLink );
		astCurr = astNoQualLink;
		while( astNext ){
			astCurr = astNext;
			astNext = stLinkTypeInfoAccess.next.get( astCurr );
		}
		stLinkTypeInfoAccess.next.set( astCurr, astLinkType );
	}
	/* element  */
	astEleType = stTypeInfoAccess.elementType.get( astCCType );
	astEleLinkType = cg_addNewTypeInfo( astEleType );
	/* add to tree  */
//	stTree.astMemChain = stTypeInfoAccess.memChain.get( astCCType );
//	stTree.astNext = stTypeInfoAccess.next.get( astCCType );
	stTree.astNoQual = astNoQualLink;
	stTree.astOrigin = astEleLinkType;
//	stTree.ucIntorFloat = stTypeInfoAccess.intFloat.get( astCCType );
	stTree.ucKind = stTypeInfoAccess.typeKind.get( astCCType );
//	stTree.ucPrecision = stTypeInfoAccess.longShortFlg.get( astCCType );
//	stTree.ucSigned = stTypeInfoAccess.signFlag.get( astCCType );
	stTree.ucTypeQualifier = stTypeInfoAccess.typeQual.get( astCCType );
	stTree.ucVariableSize = stTypeInfoAccess.bVariableSize.get( astCCType );
	stTree.ulSize = stTypeInfoAccess.size.get( astCCType );
	stTree.ucUndefined = stTypeInfoAccess.bSizeUnDefined.get( astCCType );
//	stTree.astTu = CCoreGetCurrTranslatioUnit();
	astLinkType = stLinkTypeInfoAccess.addTypeInfo( astLinkType, &stTree, cgTypeComp_forTree );

	stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
	stLinkTypeInfoAccess.elementType.set( astLinkType, stTree.astOrigin );
	stLinkTypeInfoAccess.typeKind.set( astLinkType, stTree.ucKind  );
	stLinkTypeInfoAccess.typeQual.set( astLinkType, stTree.ucTypeQualifier  );
	stLinkTypeInfoAccess.size.set( astLinkType, stTree.ulSize );
	stLinkTypeInfoAccess.bSizeUnDefined.set( astLinkType, stTree.ucUndefined );
	stLinkTypeInfoAccess.bVariableSize.set( astLinkType, stTree.ucVariableSize );
	return astLinkType;
}
static AST cg_ConvertBasicTypeInfo( AST astCCType ){
	AST  astLinkType=0;
	AST  astNoQualCc = 0;
	AST  astNoQualLink = 0;
	AST  astNewRelation;
	AST  astNext;
	AST  astCurr;
	LINKTYPE_COMP   stTree={0};
	BOOL bRet;

	/* added ? */
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astNewRelation );
	if( bRet == TRUE ){
		astLinkType = stCgTypeRelationAccess.linkedType.get( astNewRelation );
		if( astLinkType != 0 ){
			return astLinkType;
		}
	}
	/* new one  */
	stLinkTypeInfoAccess.alloc(1);
	astLinkType = stLinkTypeInfoAccess.getMax();

	astNewRelation = stCgTypeRelationAccess.addCcType( astCCType );
	stCgTypeRelationAccess.linkedType.set( astNewRelation, astLinkType );

	/* no qualifier type  */
	astNoQualCc = stTypeInfoAccess.noQualType.get( astCCType );
	if( astNoQualCc ){
		astNoQualLink = cg_addNewTypeInfo( astNoQualCc );
		stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
		astNext = stLinkTypeInfoAccess.next.get( astNoQualLink );
		astCurr = astNoQualLink;
		while( astNext ){
			astCurr = astNext;
			astNext = stLinkTypeInfoAccess.next.get( astCurr );
		}
		stLinkTypeInfoAccess.next.set( astCurr, astLinkType );
	}

	/* add to tree  */
//	stTree.astMemChain = stTypeInfoAccess.memChain.get( astCCType );
//	stTree.astNext = stTypeInfoAccess.next.get( astCCType );
	stTree.astNoQual = astNoQualLink;
//	stTree.astOrigin = stTypeInfoAccess.elementType.get( astCCType );
	stTree.ucIntorFloat = stTypeInfoAccess.intFloat.get( astCCType );
	stTree.ucKind = stTypeInfoAccess.typeKind.get( astCCType );
	stTree.ucPrecision = stTypeInfoAccess.longShortFlg.get( astCCType );
	stTree.ucSigned = stTypeInfoAccess.signFlag.get( astCCType );
	stTree.ucTypeQualifier = stTypeInfoAccess.typeQual.get( astCCType );
//	stTree.ucVariableSize = stTypeInfoAccess.bVariableSize.get( astCCType );
	stTree.ulSize = stTypeInfoAccess.size.get( astCCType );
//	stTree.astTu = CCoreGetCurrTranslatioUnit();
	astLinkType = stLinkTypeInfoAccess.addTypeInfo( astLinkType, &stTree, cgTypeComp_forTree );

	stLinkTypeInfoAccess.noQualType.set( astLinkType, astNoQualLink );
	stLinkTypeInfoAccess.intFloat.set( astLinkType, stTree.ucIntorFloat );
	stLinkTypeInfoAccess.typeKind.set( astLinkType, stTree.ucKind  );
	stLinkTypeInfoAccess.longShortFlg.set( astLinkType, stTree.ucPrecision  );
	stLinkTypeInfoAccess.signFlag.set( astLinkType, stTree.ucSigned  );
	stLinkTypeInfoAccess.typeQual.set( astLinkType, stTree.ucTypeQualifier  );
	stLinkTypeInfoAccess.size.set( astLinkType, stTree.ulSize );
	return astLinkType;
}
static AST  cg_addNewTypeInfo( AST astCCType ){
	switch( stTypeInfoAccess.typeKind.get( astCCType )){
		case TYPEKIND_BASIC			:	/* 通常の型			*/
			return cg_ConvertBasicTypeInfo( astCCType );
		case TYPEKIND_ARRAY			:	/* 配列				*/
			return cg_ConvertArrayTypeInfo( astCCType );
		case TYPEKIND_POINTER		:	/* ﾎﾟｲﾝﾀ			*/
			return cg_ConvertPointerTypeInfo( astCCType );
		case TYPEKIND_STRUCT		:	/* 構造体			*/
		case TYPEKIND_UNION			:	/* 共用体			*/
		case TYPEKIND_ENUM			:	/* 列挙型			*/
			return cg_ConvertStUnEmTypeInfo( astCCType );
		case TYPEKIND_FUNCTION		:	/* 関数(実体)			*/
			return cg_ConvertFunctionTypeInfo( astCCType );
		case TYPEKIND_BITFIELD		:	/* BitField型			*/
			return cg_ConvertBitfieldTypeInfo( astCCType );
	}
	return 0;
}
AST  CG_ConvertTypeInfo( AST astCCType ){
	BOOL bRet;
	AST astLinkType;
	TRY;
	bRet = stCgTypeRelationAccess.findCcType( astCCType, &astLinkType );
	if( bRet == TRUE ){
		return stCgTypeRelationAccess.linkedType.get( astLinkType );
	}
	CATCH(0);
	return cg_addNewTypeInfo( astCCType );
}


